unit ClienteDM;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, DB, FileUtil, LResources, Forms, Controls, Graphics,
  Dialogs, ZDataset, ZSqlUpdate, MainDM, QueryConditions;

type

  { TClienteData }

  TClienteData = class(TMainDataModule)
    ClienteDatasource: TDatasource;
    ClienteZQuery: TZQuery;
    ClienteZQuerybairro: TStringField;
    ClienteZQuerycaixa_postal: TStringField;
    ClienteZQuerycep: TStringField;
    ClienteZQuerycomplemento: TStringField;
    ClienteZQuerydesc_cidade: TStringField;
    ClienteZQueryemail: TStringField;
    ClienteZQueryendereco: TStringField;
    ClienteZQueryid_cidade: TLargeintField;
    ClienteZQueryid_cliente: TLargeintField;
    ClienteZQueryid_pessoa: TLargeintField;
    ClienteZQuerynome_pessoa: TStringField;
    ClienteZQuerynota: TStringField;
    ClienteZQuerynumero: TLongintField;
    ClienteZQuerystatus_cliente: TStringField;
    ClienteZQuerytelefone_celular: TStringField;
    ClienteZQuerytelefone_fax: TStringField;
    ClienteZQuerytelefone_fixo: TStringField;
    ClienteZQuerytipo: TStringField;
    ClienteZQueryuf: TStringField;
    ClienteZUpdate: TZUpdateSQL;
    AuxiliarZQuery: TZQuery;
    AuxiliarDatasource: TDatasource;
    procedure DataModuleCreate(Sender: TObject);
    /// Abre o DataSet
    procedure OpenDataSet(); override;
    /// Fecha o DataSet
    procedure CloseDataSet(); override;
    /// Realiza busca
    /// @param(Condicoes são as condições para a busca)
    /// @param(Tabela é a tabela no banco de dados)
    procedure Search(Condicoes: TQueryConditions; Tabela: string); override;
    procedure AlteraStatus();
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  ClienteData: TClienteData;

implementation

uses ConnectionDM;

procedure TClienteData.DataModuleCreate(Sender: TObject);
begin
  MainDataSet := ClienteZQuery;
  AlteraStatus();
end;

procedure TClienteData.OpenDataSet;
begin
  ConnectionData.EmptyQuery(ClienteZQuery, 'cliente');
end;

procedure TClienteData.CloseDataSet;
begin
  ClienteZQuery.Close();
end;

procedure TClienteData.Search(Condicoes: TQueryConditions; Tabela: string);
begin
  ConnectionData.OpenQueryConditions(ClienteZQuery, Tabela, Condicoes);
end;

procedure TClienteData.AlteraStatus();
var
  I: integer;
begin
  with AuxiliarZQuery do begin
    Close();
    SQL.Clear();
    SQL.Add('Select id_cliente from duplicata_receber where data_vencimento<current_date AND status =:A group by id_cliente');
    ParamByName('A').AsString := 'A';
    Open();
  end;
  with ClienteZQuery do begin
    for I := 1 to AuxiliarZQuery.RecordCount do begin
      AuxiliarZQuery.RecNo := I;
      Close();
      SQL.Clear();
      SQL.Add('UPDATE cliente set status_cliente=:status where id_cliente=:id_cliente');
      ParamByName('status').AsString := 'I';
      ParamByName('id_cliente').AsString :=
        AuxiliarZQuery.FieldValues['id_cliente'];
      ExecSQL();
    end;
  end;
end;

initialization
  {$I clientedm.lrs}

end.
